﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace AnLe.Models
{
    public class Connection
    {
        private static SqlConnection sqlConnection = null;
        private string conectionString = ConfigurationManager.ConnectionStrings["anleConnectionString"].ConnectionString;
        public Connection()
        {
            sqlConnection = new SqlConnection(conectionString);
        }

        public DataTable GetDataTable(SqlCommand cmd)
        {
            try
            {
                DataTable dataTable = new DataTable();
                cmd.Connection = sqlConnection;
                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(cmd);
                sqlDataAdapter.Fill(dataTable);
                return dataTable;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void open()
        {
            try
            {
                if (sqlConnection != null)
                {
                    sqlConnection.Open();
                }
                else
                {
                    sqlConnection = new SqlConnection(conectionString);
                    sqlConnection.Open();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void close()
        {
            try
            {
                sqlConnection.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}